package com.xinsoft.controller.qua;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.QuaPatrolDTO;
import com.xinsoft.entity.dto.QuaPatrolParam;
import com.xinsoft.entity.po.QuaPatrol;
import com.xinsoft.entity.vo.QuaPatrolVO;
import com.xinsoft.service.QuaPatrolService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName QuaPatrolController
 * @Description 巡检执行
 * @Author lijie
 * @Date 2023-8-2 09:52:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/qua/partrol")
@Api(value = "QuaPatrolController", tags = {"质量管理--巡检执行控制层"})
public class QuaPatrolController {
	@Autowired
	private QuaPatrolService patrolService;

	/**
	 * @Author Li9527
	 * @Description 巡检执行-暂存
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("暂存")
	@PostMapping("/add")
	public Response<?> add(@RequestBody QuaPatrolDTO quaPatrolDTO) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolDTO);
		QuaPatrol oldPatrol = patrolService.getOne(new QueryWrapper<QuaPatrol>().eq("patrol_name",quaPatrolDTO.getPatrolName()));
		if(oldPatrol != null && !oldPatrol.getId().equals(quaPatrolDTO.getId())) {
			return Response.fail("该项巡检执行已存在,请不要重复添加");
		}
		patrolService.tempSave(quaPatrolDTO);
		return Response.succeed("操作成功");
	}

	/**
	 * @Author Li9527
	 * @Description 巡检执行-提交
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("提交")
	@PostMapping("/submit")
	public Response<?> submit(@RequestBody QuaPatrolDTO quaPatrolDTO) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolDTO);
		QuaPatrol oldPatrol = patrolService.getOne(new QueryWrapper<QuaPatrol>().eq("patrol_name",quaPatrolDTO.getPatrolName()));
		if(oldPatrol != null && !oldPatrol.getId().equals(quaPatrolDTO.getId())){
			return Response.fail("该项巡检执行已存在,请不要重复添加");
		}
		patrolService.submit(quaPatrolDTO);
		return Response.succeed("操作成功");
	}

	/**
	 * @Author Li9527
	 * @Description 巡检执行-完成
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("完成")
	@PostMapping("/finish")
	public Response<?> finish(@RequestBody QuaPatrolDTO quaPatrolDTO) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolDTO);
		patrolService.finish(quaPatrolDTO);
		return Response.succeed("操作成功");
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		QuaPatrolVO quaPatrolVO = patrolService.getByInfoById(id);
		return Response.succeed(quaPatrolVO);
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		patrolService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 巡检执行分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	@ApiOperation("分页")
	public Response<?> pageList(@Valid @RequestBody QuaPatrolParam param) {
		return Response.succeed(patrolService.findPageListByParam(param));
	}

	/**
	 * @Author Li9527
	 * @Description 关闭巡检任务
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@GetMapping("/close")
	@ApiOperation("关闭")
	public Response<?> close(@RequestParam(value = "id", defaultValue = "", required = true) Integer id,
							 @RequestParam(value = "closeReason", defaultValue = "", required = true) String closeReason) {
		QuaPatrol quaPatrol = patrolService.getById(id);
		if(quaPatrol == null){
			return Response.fail("巡检任务不存在");
		}
		quaPatrol.setPatrolStatus(Const.UPKEEP_STATUS_CLOSE);
		quaPatrol.setCloseReason(closeReason);
		patrolService.updateById(quaPatrol);
		return Response.succeed();
	}

}
